package com.sunda.spmsorder.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sunda.spmsorder.entity.ShoppingCart;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 购物车表 Mapper 接口
 * </p>
 *
 * @author Wayne
 * @since 2020-11-25
 */
@Mapper
@Component
public interface ShoppingCartMapper extends BaseMapper<ShoppingCart> {

    @Select("SELECT s.WORK_CODE AS \"userId\",\n" +
            "  s.MATERIAL_NO AS \"materialNo\",\n" +
            "  s.QUANTITY AS \"quantity\",\n" +
            "  s.CREATE_TIME AS \"createTime\",\n" +
            "  s.UPDATE_TIME AS \"updateTime\",\n" +
            "  s.LONG_CONTRACT_CODE AS \"longContractCode\",\n" +
            "  s.BRAND AS \"brand\",\n" +
            "  sap.MATERIAL_ZH_DESC AS \"materialZhDesc\",\n" +
            "  sap.MATERIAL_EN_DESC AS \"materialEnDesc\",\n" +
            "  sap.SUPPLY_DESC AS \"supplyDesc\",\n" +
            "  sap.BASIC_UNIT AS \"basicUnit\",\n" +
            "  srm.SUPPLIER_CODE AS \"supplierCode\",\n" +
            "  srm.SUPPLIER_NAME AS \"supplierName\",\n" +
            "  srm.PROCUREMENT_ORGANIZATION AS \"procurementOrganization\",\n" +
            "  srm.MIN_ORDER_QUANTITY AS \"minOrderQuantity\",\n" +
            "  srm.MIN_PACK_QUANTITY AS \"minPackQuantity\",\n" +
            "  srm.SOURCE_GOODS AS \"sourceGoods\"\n" +
            "FROM SHOPPING_CART s\n" +
            "  LEFT JOIN MATERIAL_SAP sap ON s.MATERIAL_NO = sap.MATERIAL_NO\n" +
            "  LEFT JOIN MATERIAL_SRM_LONG_CONTRACT srm ON s.LONG_CONTRACT_CODE = srm.LONG_CONTRACT_CODE\n" +
            "WHERE s.WORK_CODE = #{userId} ORDER BY s.CREATE_TIME DESC")
    List<Map<String, Object>> getShoppingCartList(@Param("userId") String userId);

    @Insert("<script>INSERT INTO SHOPPING_CART(WORK_CODE, MATERIAL_NO, QUANTITY, LONG_CONTRACT_CODE, BRAND) " +
            "   SELECT A.* FROM (" +
            "       <foreach collection='shoppingCartList' item='shoppingCart' index='index' separator='UNION ALL' >" +
            "       SELECT #{shoppingCart.userId}, " +
            "           #{shoppingCart.materialNo}, " +
            "           #{shoppingCart.quantity}, " +
            "           #{shoppingCart.longContractCode}, " +
            "           #{shoppingCart.brand} " +
            "       FROM DUAL" +
            "       </foreach>" +
            "   ) A" +
            "</script>")
    void insertShoppingCart(@Param("shoppingCartList") List<ShoppingCart> shoppingCartList);

    @Insert("INSERT INTO SHOPPING_CART(WORK_CODE, MATERIAL_NO, QUANTITY, LONG_CONTRACT_CODE, BRAND)\n" +
            "VALUES (#{userId}, #{materialNo}, #{quantity}, #{longContractCode}, #{brand})")
    void insertCart(@Param("userId") String userId, @Param("materialNo") String materialNo,
                    @Param("quantity") double quantity, @Param("longContractCode") String longContractCode, @Param("brand") String brand);
}




